<?php namespace Admin\Api;


class User{
	protected $error = '未知错误';
	
	protected $db;
	
	public function __construct()
	{
		$this->db = new \Admin\Model\User;
	}
	
	public function login()
	{
		$user = $this->db->where('username',$_POST['username'])->first();
		

		
		if(!$user)
		{
			api('Admin/Api/Log/save',$_POST['username'],'登录失败');
			$this->error = '账号不存在';
			return array('code'=>1,'message'=>$this->getError());
		}
		
		if($user['password']!=Q('post.password','','md5'))
		{
			api('Admin/Api/Log/save',$_POST['username'],'登录密码错误');
			$this->error = '密码错误';
			return array('code'=>1,'message'=>$this->getError());
		}
		
		$_SESSION['username'] = $user['username'];
		$_SESSION['id'] = $user['id'];
		api('Admin/Api/Log/save',$_POST['username'],'登录');

		return array('code'=>0,'message'=>'登录成功');
	}
	
	
	
	public function getError()
	{
		return $this->db->getError()?:$this->error;
	}
	
	
	
	
}

